#include "basic.h"


int* enstablish_tcp_connection(char* ip,int port) {
	  int		         	*sockfd=malloc(sizeof(int));
	  int stat=-1;                                          // per gestire errori di connessione senza chiudere in modo grave
	  struct sockaddr_in    servaddr;

	  if ((*sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
	    printf("errore in socket\n");
	    return stat;
	  }

	    memset((void *)&servaddr, 0, sizeof(servaddr));
	    servaddr.sin_family = AF_INET;
	    servaddr.sin_port = htons(port);
	    if (inet_aton(ip, &servaddr.sin_addr) <= 0) {
	    printf("errore in inet_aton per %s\n", ip);
	    return stat;
	  }

	  if(connect(*sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0) {
		  printf("Errore in connessione al bootstrap\n");
		  free(sockfd);
		  return stat;
	  }
	  return sockfd; // va fatta free dopo averla usata
}
